Passed
Push — master ( 501c3e...cc7abf )
by Night
01:09
created

arrayFuncs.add   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
/** global: UB */
2
3
/*! ARRAY UTILS */
4
5
var arrayFuncs = {
6
7
	merge: function(){
8
		var arr = this;
9
		var val1 = arr[0];
10
11
		// if array of strings
12
		if (val1.isString()){
13
			return val1.join("");
14
		}
15
16
		// if array of arrays
17
		if (val1.isArray()){
18
			var merged = [];
19
			for (var a = 0, al = arr.length; a<al; a++){
20
				merged.addArray(arr[a]);
21
			}
22
			return merged;
23
		}
24
25
		// if array of Buffers
26
		//removeIf(nodejs)
27
		if (val1 instanceof Buffer){
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
29
			// calc the total length of all buffers
30
			var len = 0;
31
			for(var b = 0, bl = buffers.length; b < bl; b++) {
0 ignored issues
show
Bug introduced by
The variable buffers seems to be never declared. If this is a global, consider adding a /** global: buffers */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
				len += buffers[b].length;
33
			}
34
35
			// create a new buffer of that length
36
			var mega = new Buffer(len);
37
38
			// write all buffers into the mega buffer
39
			var cur = 0;
40
			for(var b = 0, bl = buffers.length; b < bl; b++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable b already seems to be declared on line 31. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Comprehensibility Naming Best Practice introduced by
The variable bl already seems to be declared on line 31. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
41
				buffers[b].copy(mega, cur, 0);
42
				cur += buffers[b].length;
43
			}
44
			return mega;
45
		}
46
		//endRemoveIf(nodejs)
47
48
		return null;
49
	},
50
	
51
	indexOf: function(value){
52
		var list = this;
53
		if (list.length === 0) {
54
			return -1;
55
		}
56
		for (var a = 0, al = list.length;a<al;a++){
57
			if (list[a] == value){
58
				return a;
59
			}
60
		}
61
		return -1;
62
	},
63 View Code Duplication
	lastIndexOf: function(value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64
		var list = this;
65
		if (list.length === 0) {
66
			return -1;
67
		}
68
		for (var a = list.length-1;a>=0;a--){
69
			if (list[a] == value){
70
				return a;
71
			}
72
		}
73
		return -1;
74
	},
75
	isEqual: function(list2){
76
		var list = this;
77
		if (list == null || list2 == null) {
2 ignored issues
show
Best Practice introduced by
Comparing list2 to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
78
			return list == list2;
79
		}	
80
		if(list.length != list2.length){
81
			return false;
82
		}
83
		
84
		var len = list.length;
85
		
86
		for (var i = 0;i<len;i++){
87
			if(list[i] !== list2[i]){
88
				return false;
89
			}
90
		}
91
		
92
		return true;
93
	},
94
	isNotEqual: function(list2){
95
		var list = this;
96
		return !IsEqual(list, list2);
97
	},
98
99
	exists: function(){
100
		var list = this;
101
		return list != null && list.length > 0;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
102
	},
103
	isFirst: function(value){
104
		var list = this;
105
		return list != null && list[0] == value;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
106
	},
107
	isLast: function(value){
108
		var list = this;
109
		return list != null && list[list.length - 1] == value;
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the != operator is not safe. Consider using !== instead.
Loading history...
110
	},
111 View Code Duplication
	count: function(value, not = false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
112
		var list = this;
113
		var total = 0;
114
		for (var a = 0, al = list.length;a<al;a++){
115
			if (not) {
116
				if (list[a] != value) {
117
					total++;
118
				}
119
			}else {
120
				if (list[a] == value) {
121
					total++;
122
				}
123
			}
124
		}
125
		return total;
126
	},
127
	or: function(list2){
128
		var list = this;
129
		if (Exists(list)) {
130
			return list;
131
		}
132
		return list2;
133
	},
134
	
135
	part: function(start, end){
136
		var list = this;
137
		
138
		// quickly exit if no items or no results possible
139
		if (list == null || list.length === 0 || start > end || start >= list.length) {
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
140
			return [];
141
		}
142
		
143
		// just get the part we need
144
		return list.slice(start, end + 1);
145
	},
146
	
147
	swap: function(slot1, slot2){
148
		var list = this;
149
		var temp = list[slot2];
150
		list[slot2] = list[slot1];
151
		list[slot1] = temp;
152
	},
153
154
	add: function(item){
155
		var list = this;
156
		list.push(item);
157
		return list.length - 1;
158
	},
159
	addToStart: function(item){
160
		var list = this;
161
		list.unshift(item);
162
		return 0;
163
	},
164
	/** return false if item exists, true if item does not */
165
	addOnce: function(value){
166
		var list = this;
167
		
168
		// return false if item exists
169
		if (list.indexOf(value) > -1) {
170
			return false;
171
		}
172
		
173
		// add if not found
174
		list.push(value);
175
		return true;
176
	},
177
	
178
	addArray: function(toAdd, addAtSlot = -1, modifyMain = true){
179
		var list = this;
180
		
181
		if (toAdd != null && toAdd.length > 0) {
1 ignored issue
show
Best Practice introduced by
Comparing toAdd to null using the != operator is not safe. Consider using !== instead.
Loading history...
182
			if (!modifyMain) {
183
				list = list.concat(); /// shallow clone
184
			}
185
			
186
			// set length first so allocates memory (maybe?)
187
			var origLen = list.length;
188
			if (addAtSlot == -1){
189
				list.length += toAdd.length;
190
			}
191
			
192
			// set all slots
193
			var next = addAtSlot == -1 ? origLen : addAtSlot;
194
			for (var a = 0, al = toAdd.length;a<al;a++, next++){
195
				list[next] = toAdd[a];
196
			}
197
		}
198
		return list;
199
	},
200
	
201
	/** adds the given value many times */
202
	addManyTimes: function(val, times){
203
		var list = this;
204
		
205
		if (times <= 0) {
206
			return list;
207
		}
208
		
209
		var n = list.length;
210
		for (var t = 0;t<times;t++){
211
			list[n++] = val;
212
		}
213
		
214
		return list;
215
	},
216
	
217
	addRange: function(toAdd, startSlot, endSlot){
218
		var list = this;
219
		
220
		// exit if no work
221
		if (endSlot < startSlot) {
222
			return;
223
		}
224
		
225
		// per wanted slot of the `add` array
226
		startSlot = startSlot.limitToArray(toAdd);
227
		endSlot = endSlot.limitToArray(toAdd);
228
		for (var s = startSlot;s <= endSlot;s++){
229
			
230
			// add into `main` array
231
			list.push(toAdd[s]);
232
			
233
		}
234
	},
235
	
236
	/** returns final index of added item, or index of already existing item */
237
	findOrAdd: function(value){
238
		var list = this;
239
		
240
		// return index of item if exists
241
		var i = list.indexOf(value);
242
		if (i > -1) {
243
			return i;
244
		}
245
		
246
		// add if not found
247
		i = list.length;
248
		list[i] = value;
249
		return i;
250
	},
251
	
252
	insertOne: function(item, slot){
253
		var list = this;
254
		
255
		// adds one slot at the given point
256
		// modifies the main array
257
		
258
		list.splice(slot, 0, item);
259
	},
260
261
	/** if slot = -1 or outside the array, the item is added to the END of the array.
262
	 * Otherwise the item is added at the given slot. */
263
	insertOneOrAdd: function(item, slot){
264
		var list = this;
265
		
266
		// adds one slot at the given point
267
		// modifies the main array
268
		
269
		if (slot < 0 || slot >= list.length) {
270
			list.push(item);
271
			return list.length - 1;
272
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
273
			list.splice(slot, 0, item);
274
			return slot;
275
		}
276
	},
277
	insertArray: function(newItems, slot, returnNew = true){
278
		var list = this;
279
		
280
		// adds many slots at the given point
281
		// returns a new array
282
		if(returnNew){
283
			return list.slice(0, slot).concat(newItems).concat(list.slice(slot));
284
		}
285
		
286
		// adds many slots at the given point
287
		// modifies the main array
288
		for (var a = 0, al = newItems.length;a<al;a++){
289
			list.splice(slot++, 0, newItems[a]);
290
		}
291
		return list;
292
	},
293
	insertArrayAfter: function(newItems, after){
294
		var list = this;
295
		if (list.isLast(after)) {
296
			list.addArray(newItems);
297
		} else {
298
			var i = list.indexOf(after);
299
			list.insertArray(i + 1, newItems);
300
		}
301
	},
302
	
303
304
	removeAndInsert: function(from, to){
305
		var list = this;
306
		
307
		// ensure slots within array
308
		var al = list.length;
309
		from = from.limitTo(0, al - 1);
310
		to = to.limitTo(0, al - 1);
311
		if (to < from) {
312
			var i1 = to;
313
			var i2 = from;
314
		}else {
315
			var i1 = from;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i1 already seems to be declared on line 312. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
316
			var i2 = to;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i2 already seems to be declared on line 313. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
317
		}
318
		
319
		// fill unaffected header
320
		var result = [];
321
		if(i1 > 0){
322
			for (var a = 0;a<i1;a++){
323
				result[a] = list[a];
324
			}
325
		}
326
		
327
		// fill "to"
328
		result[to] = list[from];
329
		
330
		// fill between from and to
331
		if(to < from){
332
			for (a = to + 1; a <= from; a++) {
333
				result[a] = list[a - 1];
334
			}
335
		}else {
336
			for (a = from; a < to; a++) {
337
				result[a] = list[a + 1];
338
			}
339
		}
340
		
341
		// fill unaffected footer
342
		for (var a = i2 + 1;a<al;a++){
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable a already seems to be declared on line 322. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
343
			result[a] = list[a];
344
		}
345
		
346
		return result;
347
	},
348
	
349
	/** replace a range of items with a given array */
350
	replaceRange: function(replaceStartSlot, replaceEndSlot, newItems, returnNew = true){
351
		var list = this;
352
		
353
		
354
		// simply remove a single item
355
		if (replaceStartSlot == replaceEndSlot && newItems.length === 0) {
356
			if (returnNew) {
357
				list = list.concat();
358
			}
359
			list.splice(replaceStartSlot, 1);
360
			return list;
361
			
362
		// simply replace a single item
363
		}else if (replaceStartSlot == replaceEndSlot && newItems.length == 1) {
1 ignored issue
show
Best Practice introduced by
Comparing newItems.length to 1 using the == operator is not safe. Consider using === instead.
Loading history...
364
			if (returnNew) {
365
				list = list.concat();
366
			}
367
			list[replaceStartSlot] = newItems[0];
368
			return list;
369
			
370
		}else{
371
			
372
			// alt method if returning a new array
373
			if(returnNew){
374
				return list.slice(0, replaceStartSlot).concat(newItems).concat(list.slice(replaceEndSlot + 1));
375
			}
376
			
377
			// remove many
378
			list.splice(replaceStartSlot, (replaceEndSlot - replaceStartSlot) + 1);
379
			
380
			// insert many
381
			var slot = replaceStartSlot;
382
			for (var a = 0, al = newItems.length;a<al;a++){
383
				list.splice(slot++, 0, newItems[a]);
384
			}
385
		}
386
		return list;
387
	},
388
	
389
	replace: function(find, replace, stringReplace = false){
390
		var list = this;
391
		for (var i = 0, il = list.length;i<il;i++){
392
			if (stringReplace){
393
394
				// replace substring within string items
395
				var str = list[i];
396
				if (str && str.constructor === String) {
397
					list[i] = str.replaceAll(find, replace);
398
				}
399
			}else{
400
401
				// replace entire items
402
				if (list[i] == find) {
403
					list[i] = replace;
404
				}
405
			}
406
		}
407
	},
408
	replaceOnce: function(find, replace){
409
		var list = this;
410
		for (var i = 0, il = list.length;i<il;i++){
411
			if (list[i] == find) {
412
				list[i] = replace;
413
				return;
414
			}
415
		}
416
	},
417
	replaceMany: function(findArray, replaceArray){
418
		var list = this;
419
		if (findArray.length != replaceArray.length){
420
			return;
421
		}
422
		for (var i = 0, il = list.length;i<il;i++){
423
			for (var f = 0, fl = findArray.length;f<fl;f++){
424
				var find = findArray[f];
425
				if (list[i] == find) {
426
					list[i] = replaceArray[f];
427
					break;
428
				}
429
			}
430
		}
431
	},
432
	
433
	
434
	remove: function(value, stringReplace = false){
435
		var list = this;
436
		for (var i = 0, il = list.length;i<il;i++){
437
			if (stringReplace){
438
439
				// remove substring within string items
440
				var str = list[i];
441
				if (str && str.constructor === String) {
442
					list[i] = str.removeAll(value);
443
				}
444
			}else{
445
446
				// remove entire items
447
				if (list[i] == value) {
448
					list.splice(i, 1);
449
					i--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
450
					il--;
451
				}
452
			}
453
		}
454
	},
455
	removeOnce: function(val){
456
		var list = this;
457
		var i = list.indexOf(val);
458
		if (i > -1) {
459
			list.splice(i, 1);
460
		}
461
		return i;
462
	},
463
	removeFirst: function(returnNew = false){
464
		var list = this;
465
		if (returnNew) {
466
			return list.slice(1);
467
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
468
			if (list.length > 0) {
469
				list.splice(0, 1);
470
			}
471
			return list;
472
		}
473
	},
474
	removeFirstX: function(count, returnNew = false){
475
		var list = this;
476
		
477
		// if fewer items than wanted, clear entire array
478
		if (list.length < count) {
479
			if (returnNew) {
480
				return [];
481
			}
482
			list.length = 0;
483
			return list;
484
		}
485
		
486
		// delete X items from start
487
		if (returnNew) {
488
			return list.slice(count);
489
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
490
			list.splice(0, count);
491
			return list;
492
		}
493
	},
494
	removeLast: function(returnNew = false){
495
		var list = this;
496
		if (returnNew) {
497
			return list.slice(0, list.length - 1);
498
		}
499
		if (list.length > 0) {
500
			list.splice( - 1, 1);
501
		}
502
		return list;
503
	},
504
	removeLastX: function(count, returnNew = false){
505
		var list = this;
506
		
507
		// if fewer items than wanted, clear entire array
508
		if (list.length < count) {
509
			if (returnNew) {
510
				return [];
511
			}
512
			list.length = 0;
513
			return list;
514
		}
515
		
516
		// delete X items from end
517
		if (returnNew) {
518
			return list.slice(0, list.length - count);
519
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
520
			list.splice( -count, count);
521
			return list;
522
		}
523
	},
524
	
525
	removeEdges: function(fromLeftEdge, fromRightEdge){
526
		var list = this;
527
		if ((list.length - fromLeftEdge - fromRightEdge) <= 0) {
528
			return [];
529
		}
530
		return list.slice(fromLeftEdge, list.length - fromRightEdge);
531
	},
532
	
533
	contains: function(value){
534
		var list = this;
535
		if (list.length == 1) {
1 ignored issue
show
Best Practice introduced by
Comparing list.length to 1 using the == operator is not safe. Consider using === instead.
Loading history...
536
			return list[0] == value;
537
		}
538
		return list.indexOf(value) > -1;
539
	},
540
	containsAny: function(values){
541
		var list = this;
542
		return IndexOfAny(list, values) > -1;
543
	},
544
	containsAll: function(values){
545
		var list = this;
546
		
547
		// exit if either null
548
		if (list == null || values == null || list.length === 0 || values.length === 0) {
2 ignored issues
show
Best Practice introduced by
Comparing values to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
549
			return false;
550
		}
551
		
552
		// check if all found
553
		for (var f = 0, fl = values.length;f<fl;f++){
554
			if (list.indexOf(values[f]) == -1) {
555
				return false;
556
			}
557
		}
558
		return true;
559
	},
560
	
561
	
562
	splitAt: function(slot, includeSlot = false, includeInFirst = false){
563
		var list = this;
564
		if (includeSlot) {
565
			if (includeInFirst) {
566
				return [list.slice(0, slot + 1), list.slice(slot + 1)];
567
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
568
				return [list.slice(0, slot), list.slice(slot)];
569
			}
570
		}
571
		return [list.slice(0, slot), list.slice(slot+1)];
572
	},
573
	splitAtEvery: function(val){
574
		var list = this;
575
		var splits = [];
576
		var lastSlot = 0;
577
		for (var a = 0, al = list.length;a<al;a++){
578
			if (list[a] == val && a > lastSlot) {
579
				splits.push(list.slice(lastSlot, a));
580
				lastSlot = a + 1;
581
			}
582
		}
583
		return splits;
584
	},
585
	
586
	moveToTop: function(slot, returnNew = false){
587
		var list = this;
588
589
		if (returnNew){
590
591
			// ALWAYS RETURNS NEW ARRAY
592
			
593
			// exit quickly if slot not in array
594
			var al = list.length;
595
			if (slot < 0 || slot >= al) {
596
				return list.concat();
597
			}
598
			
599
			// create new array with slot on top
600
			var newArr = [list[slot]];
601
			var n = 1;
602
			
603
			// add all other slots
604
			for (var a = 0;a<al;a++){
605
				if (a != slot) {
606
					newArr[n++] = list[a];
607
				}
608
			}
609
610
			return newArr;
611
612
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
613
614
			// MODIFIES ARRAY IN PLACE
615
			
616
			// exit quickly if slot not in array
617
			var al = list.length;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable al already seems to be declared on line 594. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
618
			if (slot < 0 || slot >= al) {
619
				return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Are you sure this return statement is not missing an argument? If this is intended, consider adding an explicit undefined like return undefined;.
Loading history...
620
			}
621
			
622
			// delete and re-add slot
623
			var val = list[slot];
624
			list.splice(slot, 1);
625
			list.unshift(val);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
626
		}
627
	},
628
	moveToBottom: function(slot, returnNew = false){
629
		var list = this;
630
631
		if (returnNew){
632
633
			// ALWAYS RETURNS NEW ARRAY
634
			
635
			// exit quickly if slot not in array
636
			var al = list.length;
637
			if (slot < 0 || slot >= al) {
638
				return list.concat();
639
			}
640
			
641
			// create new array with slot on top
642
			var newArr = [];
643
			var n = 0;
644
			
645
			// add all other slots
646
			for (var a = 0;a<al;a++){
647
				if (a != slot) {
648
					newArr[n++] = list[a];
649
				}
650
			}
651
			
652
			// add slot to bottom
653
			newArr[n++] = list[slot];
0 ignored issues
show
Unused Code introduced by
The assignment to variable n seems to be never used. Consider removing it.
Loading history...
654
			return newArr;
655
656
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
657
658
			// MODIFIES ARRAY IN PLACE
659
			
660
			// exit quickly if slot not in array
661
			var al = list.length;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable al already seems to be declared on line 636. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
662
			if (slot < 0 || slot >= al) {
663
				return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Are you sure this return statement is not missing an argument? If this is intended, consider adding an explicit undefined like return undefined;.
Loading history...
664
			}
665
			
666
			// delete and re-add slot
667
			var val = list[slot];
668
			list.splice(slot, 1);
669
			list.push(val);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
670
		}
671
	},
672
	moveUp: function(item){
673
		var list = this;
674
		
675
		// MODIFIES ARRAY IN PLACE
676
		
677
		// exit quickly if slot not in array, or already on top
678
		var slot = list.indexOf(item);
679
		if (slot <= 0) {
680
			return false;
681
		}
682
		
683
		// move one up
684
		Swap(list, slot, slot - 1);
685
		return true;
686
	},
687
	moveDown: function(item){
688
		var list = this;
689
		
690
		// MODIFIES ARRAY IN PLACE
691
		
692
		// exit quickly if slot not in array, or already at bottom
693
		var al = list.length;
694
		var slot = list.indexOf(item);
695
		if (slot < 0 || slot >= (al - 1)) {
696
			return false;
697
		}
698
		
699
		// move one down
700
		Swap(list, slot, slot + 1);
701
		return true;
702
	},
703
	moveSlotUp: function(slot){
704
		var list = this;
705
		
706
		// MODIFIES ARRAY IN PLACE
707
		
708
		// exit quickly if slot not in array, or already on top
709
		var al = list.length;
710
		if (slot <= 0 || slot >= al) {
711
			return false;
712
		}
713
		
714
		// move one up
715
		Swap(list, slot, slot - 1);
716
		return true;
717
	},
718
	moveSlotDown: function(slot){
719
		var list = this;
720
		
721
		// MODIFIES ARRAY IN PLACE
722
		
723
		// exit quickly if slot not in array, or already at bottom
724
		var al = list.length;
725
		if (slot < 0 || slot >= (al - 1)) {
726
			return false;
727
		}
728
		
729
		// move one down
730
		Swap(list, slot, slot + 1);
731
		return true;
732
	},
733
	/** Move an item from the given list, to the start/end of the target list */
734
	moveToArray: function(item, toList, evenIfExists = false, addToEnd = true){
735
		var list = this;
736
		
737
		// remove from source list
738
		RemoveOne(list, item);
739
		
740
		// add to target list
741
		if (evenIfExists || toList.indexOf(item) == -1) {
742
			if (addToEnd) {
743
				toList.push(item);
744
			}else{
745
				toList.unshift(item);
746
			}
747
			return true;
748
		}
749
		
750
		return false;
751
	},
752
	beginsWithArray: function(value){
753
		return this.startsWithArray(value);
754
	},
755
	startsWithArray: function(check){
756
		var list = this;
757
		
758
		// quickly test if length sufficient
759
		var clen = check.length;
760
		var mlen = list.length;
761
		if (mlen < clen) {
762
			return false;
763
		}
764
		
765
		// check if first slots match
766
		for (var b = 0;b<clen;b++){
767
			if (list[b] != check[b]) {
768
				return false;
769
			}
770
		}
771
		return true;
772
	},
773
	endsWithArray: function(check){
774
		var list = this;
775
		
776
		// quickly test if length sufficient
777
		var clen = check.length;
778
		var mlen = list.length;
779
		if (mlen < clen) {
780
			return false;
781
		}
782
		
783
		// check if last slots match
784
		var off = (mlen - clen);
785
		for (var b = 0;b<clen;b++){
786
			if (list[off + b] != check[b]) {
787
				return false;
788
			}
789
		}
790
		return true;
791
	},
792
	indexOfArray: function(containsArr){
793
		var list = this;
794
		
795
		// returns index of containing list in main array
796
		
797
		if (list.length < containsArr.length) {
798
			return -1;
799
		}
800
		
801
		var cl = containsArr.length;
802
		for (var a = 0, al = list.length - (cl - 1);a<al;a++){
803
			
804
			var allMatch = true;
805
			for (var c = 0;c<cl;c++){
806
				if (list[a+c] != containsArr[c]) {
807
					allMatch = false;
808
					break;
809
				}
810
			}
811
			
812
			if (allMatch) {
813
				return a;
814
			}
815
		}
816
		return -1;
817
	},
818
	
819
	beginsWith: function(value){
820
		return this.startsWith(value);
821
	},
822
	startsWith: function(value){
823
		var list = this;
824
		return list[0] == value;
825
	},
826
	endsWith: function(value){
827
		var list = this;
828
		return list[list.length - 1] == value;
829
	},
830
	
831
	next: function(obj, wrap = false){
832
		var list = this;
833
		if (list == null) {
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
834
			return null;
835
		}
836
		var i = list.indexOf(obj);
837
		if (i > -1) {
838
			return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1];
839
		}
840
		return null;
841
	},
842
	prev: function(obj, wrap = false){
843
		var list = this;
844
		if (list == null) {
1 ignored issue
show
Best Practice introduced by
Comparing list to null using the == operator is not safe. Consider using === instead.
Loading history...
845
			return null;
846
		}
847
		var i = list.indexOf(obj);
848
		if (i > 0) {
849
			return wrap ? list[list.length - 1] : list[i - 1];
850
		}
851
		return null;
852
	},
853
	
854
	
855
	/** Returns the nearest existing slot value in the array. Returns `ifNoSlots` if the array is empty. */
856
	within: function(slot, ifNoSlots = null){
857
		var list = this;
858
		
859
		// return null if array empty
860
		var len = list.length;
861
		if (len == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
862
			return ifNoSlots;
863
		}
864
		
865
		// return first slot if index negative
866
		if (slot < 0) {
867
			return list[0];
868
		}
869
		
870
		// return last slot if index more than last slot
871
		if (slot >= len) {
872
			return list[len - 1];
873
		}
874
		
875
		// return given slot if within array
876
		return list[slot];
877
	},
878
	
879
	first: function(list){
880
		var list = this;
881
		var len = list.length;
882
		if (len == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
883
			return null;
884
		}
885
		return list[0];
886
	},
887
	firstExisting: function(slots, blankVal = null){
888
		var list = this;
889
		for (var s = 0, sl = slots.length;s<sl;s++){
890
			var val;
891
			if ((val = list[slots[s]]) != blankVal) {
892
				return val;
893
			}
894
		}
895
		return null;
896
	},
897
	last: function(list){
898
		var list = this;
899
		var len = list.length;
900
		if (len == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
901
			return null;
902
		}
903
		return list[len-1];
904
	},
905
	lastX: function(count){
906
		var list = this;
907
		var s = (0).max(list.length - count);
908
		return list.slice(s, list.length);
909
	},
910
	setFirst: function(value){
911
		var list = this;
912
		var len = list.length;
913
		if (len == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
914
			return;
915
		}
916
		list[0] = value;
917
	},
918
	setLast: function(value){
919
		var list = this;
920
		var len = list.length;
921
		if (len == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
922
			return;
923
		}
924
		list[len-1] = value;
925
	},
926
	
927
	random: function(list){
928
		var list = this;
929
		
930
		// return null if array empty
931
		if (list.length === 0) {
932
			return null;
933
		}
934
		
935
		// return random slot within array
936
		return list[parseInt(Math.random() * 1000000) % list.length];
937
	},
938
939
	pick: function(IDs, sameSlots = false, fastAndUnsafe = false, out = null, evenIfNull = false){
940
		var list = this;
941
		if (out == null){
1 ignored issue
show
Best Practice introduced by
Comparing out to null using the == operator is not safe. Consider using === instead.
Loading history...
942
			out = [];
943
		}
944
		
945
		if (IDs){
946
			if (fastAndUnsafe) {
947
				
948
				for (var i = 0, il = IDs.length;i<il;i++){
949
					var id = IDs[i];
950
					var val = list[id];
951
					if (sameSlots) {
952
						out[id] = val;
953
					}else{
954
						out.push(val);
955
					}
956
				}
957
				
958
			}else{
959
				
960
				for (var i = 0, il = IDs.length;i<il;i++){
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable il already seems to be declared on line 948. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 948. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
961
					id = IDs[i];
962
					if (id > -1){
963
						val = list[id];
964
						if (val != null || evenIfNull) {
1 ignored issue
show
Best Practice introduced by
Comparing val to null using the != operator is not safe. Consider using !== instead.
Loading history...
965
							if (sameSlots) {
966
								out[id] = val;
967
							}else{
968
								out.push(val);
969
							}
970
						}
971
					}
972
				}
973
				
974
			}
975
		}
976
		
977
		return out;
978
	},
979
980
	/** Get the data of `list`, by searching `indexID` in `indexArr`, or return `defaultVal` if not found */
981
	getByMatchingArray: function(indexArr, indexID, defaultVal = null){
982
		var list = this;
983
		var slot = indexArr.indexOf(indexID);
984
		return slot == -1 ? defaultVal : list[slot];
985
	},
986
	/** Set the data of `list`, by searching `indexID` in `indexArr` */
987
	setByMatchingArray: function(indexArr, indexID, data){
988
		var list = this;
989
		var slot = indexArr.indexOf(indexID);
990
		if (slot == -1) {
991
			return false;
992
		}
993
		list[slot] = data;
994
		return true;
995
	},
996
	
997
	page: function(page, pageLength, invisibleRows = null){
998
		var list = this;
999
		
1000
		// ensure page no. in limits
1001
		var lastPage = Math.ceil(list.length / pageLength);
1002
		page = page.limitTo(0, lastPage - 1);
1003
		
1004
		// get first/last row in page
1005
		var pageStart = page*pageLength;/// 0-based - first row in page
1006
		var pageEnd = (pageStart + pageLength) - 1;/// 0-based - last row in page
1007
		
1008
		// get on-page rows
1009
		var visibleRows = list.getRange(pageStart, pageLength);
1010
		
1011
		// get off-page rows
1012
		if (invisibleRows) {
1013
			invisibleRows.addRange(list, 0, pageStart - 1);
1014
			invisibleRows.addRange(list, pageEnd + 1, list.length - 1);
1015
		}
1016
		
1017
		return visibleRows;
1018
	},
1019
	
1020
	trim: function(returnNew = false, trimVal = null){
1021
		var list = this;
1022
		var first = IndexOf(list, trimVal, true);
1023
		if (first == -1) {
1024
			if (returnNew) {
1025
				return [];
1026
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1027
				list.length = 0;
1028
			}
1029
		}else{
1030
			var last = list.lastIndexOf(trimVal, true);
1031
			if (returnNew) {
1032
				return GetManySE(list, first, last);
1033
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1034
				
1035
				if (first != -1 && first != 0){
1 ignored issue
show
Best Practice introduced by
Comparing first to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
1036
					DeleteManySE(list, 0, first-1);
1037
				}
1038
				if (last != -1){
1039
					list.length = last - first + 1; 
1040
				}
1041
			}
1042
		}
1043
		return list;
1044
	},
1045
1046
	trimLeft: function(returnNew = false, trimVal = null){
1047
		var list = this;
1048
		var first = IndexOf(list, trimVal, true);
1049
		if (first == -1) {
1050
			if (returnNew) {
1051
				return [];
1052
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1053
				list.length = 0;
1054
			}
1055
		}else{
1056
			if (returnNew) {
1057
				return GetAfter(list, first);
1058
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1059
				if (first != -1 && first != 0){
1 ignored issue
show
Best Practice introduced by
Comparing first to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
1060
					DeleteManySE(list, 0, first-1);
1061
				}
1062
			}
1063
		}
1064
		return list;
1065
	},
1066
	trimRight: function(returnNew = false, trimVal = null){
1067
		var list = this;
1068
		var last = IndexOf(list, trimVal, true);
1069
		if (last == -1) {
1070
			if (returnNew) {
1071
				return [];
1072
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1073
				list.length = 0;
1074
			}
1075
		}else{
1076
			if (returnNew) {
1077
				return GetBefore(list, last);
1078
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1079
				list.length = last + 1;
1080
			}
1081
		}
1082
		return list;
1083
	},
1084
	
1085
	transpose: function() {
1086
		var arr = this;
1087
		var transposed = [];
1088
		
1089
		for (var r = 0; r < arr.length; r++) {
1090
			for (var c = 0; c < arr[r].length; c++) {
1091
				if (transposed[c] == null) {
1 ignored issue
show
Best Practice introduced by
Comparing transposed.c to null using the == operator is not safe. Consider using === instead.
Loading history...
1092
					transposed[c] = [];
1093
				}
1094
				transposed[c][r] = arr[r][c];
1095
			}
1096
		}
1097
		
1098
		return transposed;
1099
	},
1100
	
1101
	none:null
1102
};
1103
1104
// register funcs
1105
UB.registerFuncs(Array.prototype, arrayFuncs);
1106